home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Streams.c
-
- Contains: Utility routines to stream data into a block of memory
-
- Written by: Chris White, Developer Technical Support
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- 9/28/95 CW First release
-
- */
-
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifdef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
-
- #ifndef __STREAMS__
- #include "Streams.h"
- #endif
-
-
-
-
-
- OSErr NewStream ( tStreamRef* streamRef, Size blockSize )
- {
- OSErr theErr;
- Ptr thePtr;
- tStreamHeaderPtr theHeader;
-
-
- *streamRef = nil;
- thePtr = NewPtr ( sizeof ( tStreamHeader ) + blockSize );
- theErr = MemError ( );
- if ( theErr )
- return theErr;
-
- theHeader = (tStreamHeaderPtr) thePtr;
- theHeader->blockSize = blockSize;
- theHeader->currentSize = sizeof ( tStreamHeader ) + blockSize;
- theHeader->cursor = sizeof ( tStreamHeader );
- theHeader->maxCursor = sizeof ( tStreamHeader );
-
- *streamRef = thePtr;
-
- return noErr;
- }
-
-
-
- OSErr DisposeStream ( tStreamRef streamRef )
- {
- OSErr theErr;
-
- DisposePtr ( streamRef );
- theErr = MemError ( );
- if ( theErr == memWZErr )
- return kInvalidStreamRef;
-
- return theErr;
- }
-
-
-
- OSErr SetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
- {
- OSErr theErr;
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
-
- /* Allocate more memory if we need to */
- if ( theHeader->cursor + dataSize > theHeader->currentSize )
- {
- SetPtrSize ( streamRef, theHeader->currentSize + theHeader->blockSize );
- theErr = MemError ( );
- if ( theErr )
- return theErr;
-
- theHeader->currentSize += theHeader->blockSize;
- }
-
- if ( dataPtr )
- BlockMoveData ( dataPtr, streamRef + theHeader->cursor, dataSize );
- theHeader->cursor += dataSize;
-
- if ( theHeader->cursor > theHeader->maxCursor )
- theHeader->maxCursor = theHeader->cursor;
-
- return noErr;
- }
-
-
-
- OSErr GetStreamData ( tStreamRef streamRef, Ptr dataPtr, Size dataSize )
- {
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
- if ( theHeader->cursor + dataSize > theHeader->maxCursor )
- return kBoundsErr;
-
- BlockMoveData ( streamRef + theHeader->cursor, dataPtr, dataSize );
- theHeader->cursor += dataSize;
-
- return noErr;
- }
-
-
-
- OSErr GetStreamDataPtr ( tStreamRef streamRef, Ptr* dataPtr )
- {
- OSErr theErr;
- Ptr thePtr;
- Size theSize;
- tStreamHeaderPtr theHeader;
-
- *dataPtr = nil;
- theHeader = (tStreamHeaderPtr) streamRef;
- theSize = GetStreamDataSize ( streamRef );
-
- thePtr = NewPtr ( theSize );
- theErr = MemError ( );
- if ( theErr )
- return theErr;
-
- BlockMoveData ( streamRef + sizeof ( tStreamHeader ), thePtr, theSize );
- *dataPtr = thePtr;
-
- return noErr;
- }
-
-
-
- Size GetStreamDataSize ( tStreamRef streamRef )
- {
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
- return theHeader->maxCursor - sizeof ( tStreamHeader );
- }
-
-
-
- tStreamCursor GetStreamCursor ( tStreamRef streamRef )
- {
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
- return theHeader->cursor;
- }
-
-
-
- OSErr SetStreamCursor ( tStreamRef streamRef, tStreamCursor cursor )
- {
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
- if ( cursor > theHeader->currentSize )
- return kBoundsErr;
- if ( cursor < sizeof ( tStreamHeader ) )
- return kBoundsErr;
-
- theHeader->cursor = cursor;
-
- return noErr;
- }
-
-
-
- void ResetStreamCursor ( tStreamRef streamRef )
- {
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
- theHeader->cursor = sizeof ( tStreamHeader );
-
- return;
- }
-
-
-
- OSErr CompactStream ( tStreamRef streamRef )
- {
- OSErr theErr;
- tStreamHeaderPtr theHeader;
-
- theHeader = (tStreamHeaderPtr) streamRef;
- SetPtrSize ( streamRef, theHeader->maxCursor );
- theErr = MemError ( );
- if ( theErr == memWZErr )
- return kInvalidStreamRef;
-
- theHeader->currentSize = theHeader->maxCursor;
-
- return theErr;
- }
-
-
-
-
-
-